package plus.logic;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import common.core.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import plus.util.Condition;
import plus.util.ConditionHelper;

import java.io.Serializable;
import java.util.*;

public class LogicBaseController< S extends LogicBaseService<T> ,T> implements  LogicBaseFeign<T>{

    @Autowired
    S logicBaseService;

    @Override
    public boolean save(@RequestBody T entity) {
        return logicBaseService.save(entity);
    }

    @Override
    public boolean saveBatch(@RequestBody Condition condition) {
        return logicBaseService.saveBatch(ConditionHelper.getEntitys(condition));
    }

    @Override
    public boolean saveOrUpdateBatch(@RequestBody Condition condition) {
        return logicBaseService.saveOrUpdateBatch(ConditionHelper.getEntitys(condition));
    }

    @Override
    public boolean removeById(@RequestParam("id")Serializable id) {
        return logicBaseService.removeById(id);
    }

    @Override
    public boolean removeByMap(@RequestBody Condition condition) {
        return logicBaseService.removeByMap(ConditionHelper.getColumnMap(condition));
    }

    @Override
    public boolean removeByEntity(T entity) {
        Map<String,Object> notEmptyMap = new HashMap<>();
        Map<String,Object> entityMap = CommonUtil.convertBeanToMap(entity);
        for (String key : entityMap.keySet()) {
            if(CommonUtil.isNotEmpty(entityMap.get(key))){
                notEmptyMap.put(key,entityMap.get(key));
            }
        }
        return logicBaseService.removeByMap(notEmptyMap);
    }

    @Override
    public boolean removeByIds(@RequestBody Condition condition) {
        return logicBaseService.removeByIds(ConditionHelper.getIdList(condition));
    }

    @Override
    public boolean updateById(@RequestBody T entity) {
        return logicBaseService.updateById(entity);
    }

    @Override
    public boolean updateBatchById(@RequestBody Condition condition) {
        return logicBaseService.updateBatchById(ConditionHelper.getEntitys(condition));
    }

    @Override
    public boolean saveOrUpdate(@RequestBody T entity) {
        return logicBaseService.saveOrUpdate(entity);
    }

    @Override
    public T getOneByMap(@RequestBody Condition condition) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        return logicBaseService.getOne((Wrapper<T>)ConditionHelper.getWrapper(condition,wrapper));
    }

    @Override
    public T getById(@RequestParam("id")Serializable id) {
        return logicBaseService.getById(id);
    }

    @Override
    public Collection<T> listByIds(@RequestBody Condition condition) {
        return logicBaseService.listByIds(ConditionHelper.getIdList(condition));
    }

    @Override
    public Collection<T> listByMap(@RequestBody Condition condition) {
        return logicBaseService.listByMap(ConditionHelper.getColumnMap(condition));
    }

    @Override
    public int count() {
        return logicBaseService.count();
    }

    @Override
    public List<T> list() {
        return logicBaseService.list();
    }

    @Override
    public List<Map<String, Object>> listMaps() {
        return logicBaseService.listMaps();
    }

    @Override
    public List<Object> listObjs() {
        return this.listObjs();
    }

    @Override
    public IPage<T> page(Condition condition) {
        QueryWrapper<T> wrapper = new QueryWrapper<>();
        return this.logicBaseService.page(ConditionHelper.getPage(condition), (QueryWrapper)ConditionHelper.getPageWrapper(condition,wrapper));
    }
}
